package com.jzy.secondhandbooktradingmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzy.secondhandbooktradingmanagement.LoginUser;
import com.jzy.secondhandbooktradingmanagement.entity.User;
import com.jzy.secondhandbooktradingmanagement.mapper.UserMapper;
import com.jzy.secondhandbooktradingmanagement.service.IUserService;
import com.jzy.secondhandbooktradingmanagement.utils.IRedisUtil;
import com.jzy.secondhandbooktradingmanagement.utils.TokenUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private IRedisUtil redisUtil;

    @Override
    public User register(User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()));
        if (res != null) {
            throw new RuntimeException("用户名已重复");
        }
        userMapper.insert(user);
        return user;
    }

    @Override
    public User login(User user) {
        User res = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, user.getUsername()).eq(User::getPassword, user.getPassword()));
        if (res == null) {
            throw new RuntimeException("用户名或密码错误");
        }
        String token = TokenUtils.genToken(res);
        res.setToken(token);
        LoginUser.addVisitCount();
        
        // 登录成功后，也可以主动缓存用户信息，或者等待 TokenUtils 懒加载
        // redisUtil.setValue("user:id:" + res.getId(), res, 30 * 60 * 1000L);
        
        return res;
    }

    @Override
    public Page<User> findPage(Integer pageNum, Integer pageSize, String search) {
        LambdaQueryWrapper<User> wrappers = Wrappers.<User>lambdaQuery();
        if (StringUtils.isNotBlank(search)) {
            wrappers.like(User::getNickName, search);
        }
        wrappers.eq(User::getRole, 2); // Assuming role 2 is normal user
        return userMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
    }

    @Override
    public Page<User> findPage2(Integer pageNum, Integer pageSize, String search1, String search2, String search3, String search4) {
        LambdaQueryWrapper<User> wrappers = Wrappers.<User>lambdaQuery();
        if (StringUtils.isNotBlank(search1)) {
            wrappers.like(User::getId, search1);
        }
        if (StringUtils.isNotBlank(search2)) {
            wrappers.like(User::getNickName, search2);
        }
        if (StringUtils.isNotBlank(search3)) {
            wrappers.like(User::getPhone, search3);
        }
        if (StringUtils.isNotBlank(search4)) {
            wrappers.like(User::getAddress, search4);
        }
        wrappers.eq(User::getRole, 2);
        return userMapper.selectPage(new Page<>(pageNum, pageSize), wrappers);
    }

    @Override
    public void updatePassword(Integer id, String password) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        User user = new User();
        user.setPassword(password);
        userMapper.update(user, updateWrapper);
        
        // 密码修改后，清除缓存，强制重新登录或重新获取信息
        redisUtil.remove("user:id:" + id);
    }
}
